package com.lovo.dao.impl;

import com.lovo.bean.UserBean;
import com.lovo.dao.IUserDao;
import org.springframework.stereotype.Repository;

import java.sql.SQLException;

@Repository
public class UserDaoImpl extends BaseDao implements IUserDao {
    @Override
    public UserBean login(String userName, String password) {
        UserBean user = null;
        this.setConnection();
        try {
            ps = con.prepareStatement("select * from t_user where u_userName=? and u_password=?");
            ps.setObject(1, userName);
            ps.setObject(2, password);
            rs = ps.executeQuery();

            if (rs.next()) {
                user = new UserBean();
                user.setUserId(rs.getInt("pk_userId"));
                user.setUserName(rs.getString("u_userName"));
                user.setPassword(rs.getString("u_password"));
                user.setMail(rs.getString("u_mail"));
                user.setImgUrl(rs.getString("u_imgUrl"));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return user;
    }

    @Override
    public void add(UserBean user) {
        this.setConnection();
        try {
            ps = con.prepareStatement("INSERT INTO t_user(u_userName,u_password,u_mail,u_imgUrl)" +
                    " VALUES(?,?,?,?)");
            ps.setObject(1,user.getUserName());
            ps.setObject(2,user.getPassword());
            ps.setObject(3,user.getMail());
            ps.setObject(4,user.getImgUrl());

            ps.executeUpdate();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            this.closeConnection();
        }
    }
}
